package com.cango.qutz.web.auth.role.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.cango.qutz.web.auth.role.service.IRoleService;
import com.cango.qutz.web.entity.MenuButtonEntity;
import com.cango.qutz.web.entity.RoleEntity;
import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.controller.BaseController;
import com.cango.framework.base.pagination.DataTableRequest;
import com.cango.framework.base.pagination.DataTableResponse;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.role.service.IRoleService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

/**
 * <p>
 * ClassName: RoleController
 * </p>
 * <p>
 * Description: 角色Controller
 * </p>
 * <p>
 * Author: weih
 * </p>
 * <p>
 * Date: 2015-04-01
 * </p>
 */
@Controller
@RequestMapping("/auth/role")
public class RoleController extends BaseController {

    /**
     * log
     */
    private final Log log = LogFactory.getLog(RoleController.class);

    /**
     * <p>
     * Field roleService: 角色Service注入
     * </p>
     */
    @Autowired
    private IRoleService roleService;

    /**
     * 功能描述: <br>
     * 到角色管理页面
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/toRoleManage")
    public String toRoleManage() {
        return "/role/roleList";
    }

    /**
     * <p>
     * Description: 分页查询角色记录
     * </p>
     * 
     * @param role 角色对象
     * @param response response
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<RoleEntity> getListData(RoleEntity role, HttpServletResponse response,
            DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), dataTable.getiDisplayLength() == -1 ? 10000
                : dataTable.getiDisplayLength());
        PaginationResult<RoleEntity> result = roleService.queryListByPage(role, pageInfo);
        return new DataTableResponse(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 查询未与某岗位关联的角色信息
     * </p>
     * 
     * @param role 角色对象
     * @param response response
     * @param dataTable 分页对象
     */
    @RequestMapping("/getRoleListData")
    @ResponseBody
    public DataTableResponse<RoleEntity> getRoleListData(RoleEntity role, HttpServletResponse response,
            DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), 10000);
        PaginationResult<RoleEntity> result = roleService.queryRoleList(role, pageInfo);
        return new DataTableResponse(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 查询与某岗位关联的角色信息
     * </p>
     * 
     * @param role 角色对象
     * @param response response
     */
    @RequestMapping("/getPositionRoleList")
    @ResponseBody
    public DataTableResponse<RoleEntity> getPositionRoleList(RoleEntity role, HttpServletResponse response,
            DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), 10000);
        PaginationResult<RoleEntity> result = roleService.queryPositionRoleList(role, pageInfo);
        return new DataTableResponse(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 跳转角色新增页面
     * </p>
     * 
     * @return
     */
    @RequestMapping("/toAddRole")
    public String toAddRole() {
        return "role/roleAdd";
    }

    @RequestMapping("/validCode")
    @ResponseBody
    public boolean validCode(String roleCode) {
        return this.roleService.validCode(roleCode);
    }

    /**
     * <p>
     * Description: 保存角色记录
     * </p>
     * 
     * @param role 角色对象
     * @param response
     */
    @RequestMapping("/saveRole")
    public void saveRole(RoleEntity role, String resources, HttpServletResponse response) {
        role.setCreateUser(getSessionUser().getUserId());
        role.setCreateTime(new Date());
        roleService.insert(role, resources);
        log.info("用户" + getSessionUser().getUserName() + "新增了角色:" + (new Gson()).toJson(role));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 跳转角色编辑页面
     * </p>
     * 
     * @param roleId 角色对象
     * @param model
     * @return
     */
    @RequestMapping("/toEditRole")
    public String toEditRole(Integer roleId, Model model) {
        RoleEntity role = this.roleService.queryObjectById(roleId);
        model.addAttribute("role", role);
        return "role/roleEdit";
    }

    /**
     * <p>
     * Description: 保存角色修改记录
     * </p>
     * 
     * @param role 角色对象
     * @param response
     */
    @RequestMapping("/editRole")
    public void editRole(RoleEntity role, String resources, HttpServletResponse response) {
        role.setUpdateUser(getSessionUser().getUserId());
        role.setUpdateTime(new Date());
        roleService.update(role, resources);
        log.info("用户" + getSessionUser().getUserName() + "修改了角色:" + (new Gson()).toJson(role));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 删除角色记录
     * </p>
     * 
     * @param role 角色对象
     * @param response
     */
    @RequestMapping("/deleteRole")
    public void deleteRole(RoleEntity role, HttpServletResponse response) {
        role.setUpdateUser(getSessionUser().getUserId());
        role.setUpdateTime(new Date());
        roleService.delete(role);
        log.info("用户" + getSessionUser().getUserName() + "删除了角色:" + (new Gson()).toJson(role));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * 功能描述: <br>
     * 获取所有资源树的数据
     *
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/getResourceTreeData")
    public void getResourceTreeData(String roleCode, HttpServletResponse response) {
        List<MenuButtonEntity> resList = this.roleService.queryMenuButtonList(roleCode);
        this.ajaxJson(response, resList);
    }

}
